package com.czh.springboot04.service.serviceimpl;

import com.czh.springboot04.entity.Consult;
import com.czh.springboot04.entity.ConsultSpecialty;
import com.czh.springboot04.entity.Specialty;
import com.czh.springboot04.mapper.ConsultMapper;
import com.czh.springboot04.mapper.ConsultSpecialtyMapper;
import com.czh.springboot04.mapper.SpecialtyMapper;
import com.czh.springboot04.service.ConsultSpecialtyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConsultSpecialtyServiceImpl implements ConsultSpecialtyService {
    @Autowired
    private ConsultSpecialtyMapper consultSpecialtyMapper;
    @Autowired
    private ConsultMapper consultMapper;
    @Autowired
    private SpecialtyMapper specialtyMapper;
    @Override
    public int insert(ConsultSpecialty consultSpecialty) {
        int j = specialtyMapper.insert(consultSpecialty.getSpecialty());
        int k = consultSpecialtyMapper.insert(consultSpecialty);
        if (j>0&&k>0){
            return k;
        }
        return 0;
    }
    @Override
    public int update(ConsultSpecialty consultSpecialty) {
        int i = consultMapper.updateConsult(consultSpecialty.getConsult());
        int j = specialtyMapper.update(consultSpecialty.getSpecialty());
        if (i>0&&j>0){
            return consultSpecialtyMapper.update(consultSpecialty);
        }
        return 0;
    }

    @Override
    public int delete(int id) {
        ConsultSpecialty consultSpecialty = consultSpecialtyMapper.selectById(id);
        if (consultSpecialty == null) {
            return 0;
        }
        int i = consultMapper.deleteConsult(consultSpecialty.getConsultId());
        int j = specialtyMapper.delete(consultSpecialty.getSpecialtyId());
        int k = consultSpecialtyMapper.delete(id);
        if (i>0&&j>0&&k>0){
            return k;
        }
        return 0;
    }

    @Override
    public PageInfo<ConsultSpecialty> selectByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
//        List<ConsultSpecialty> consultSpecialtys = consultSpecialtyMapper.selectByPage(consultSpecialty);
//        for (ConsultSpecialty consultSpecialty1 : consultSpecialtys) {
//           Consult consult = consultMapper.findConsultById(consultSpecialty1.getConsultId());
//           consultSpecialty1.setConsult(consult);
//           Specialty specialty = specialtyMapper.selectById(consultSpecialty1.getSpecialtyId());
//           consultSpecialty1.setSpecialty(specialty);
//        }
        List<ConsultSpecialty> consultSpecialtys = consultSpecialtyMapper.selectByPage();
//        null
        PageInfo<ConsultSpecialty> pageInfo = new PageInfo<>(consultSpecialtys);
        //consultSpecialtys
        return pageInfo;
    }

    @Override
    public ConsultSpecialty selectById(int id) {
        return consultSpecialtyMapper.selectById(id);
    }
    @Override
    public List<ConsultSpecialty> selectAll() {
        return consultSpecialtyMapper.selectAll();
    }
}

